package com.test.flink_source;

import com.test.beans.UserBean;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class F01Socket {

    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",8888);
        StreamExecutionEnvironment webUI = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);
//        DataStreamSource<String> data = webUI.socketTextStream("127.0.0.1", 8899, ",");
        DataStreamSource<String> data = webUI.socketTextStream("127.0.0.1", 8899);

        SingleOutputStreamOperator<UserBean> sso = data.map(line -> {
            try {
                UserBean userBean = new UserBean();
                String[] arr = line.split(",");
                userBean.setUid(Integer.parseInt(arr[0]));
                userBean.setName(String.valueOf(arr[1]));
                userBean.setAge(Integer.parseInt(arr[2]));
                userBean.setAddress(String.valueOf(arr[3]));
                userBean.setBirthday(Long.parseLong(arr[4]));
                return userBean;
            } catch (Exception e) {
                return new UserBean();
            }
        }).returns(new TypeHint<UserBean>() {
        });

        System.out.println(sso.getParallelism());
        sso.print();
        webUI.execute();
    }
}
